package com.test.sshshiro.framework;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.CustomNumberEditor;
import org.springframework.ui.ModelMap;
import org.springframework.util.Assert;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import com.test.sshshiro.framework.page.Page;
import com.test.sshshiro.framework.page.PageRequest;
import com.test.sshshiro.util.AppConstant;
import com.test.sshshiro.util.BeanUtils;
import com.test.sshshiro.util.ConvertRegisterHelper;

public class BaseController {
	protected final static String CREATED_SUCCESS = "message.create.success";
	protected final static String UPDATE_SUCCESS = "message.update.success";
	protected final static String DELETE_SUCCESS = "message.delete.success";
	
	static {
		System.out.println("Register Converter");
		ConvertRegisterHelper.registerConverters();
	}

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat sdf = new SimpleDateFormat(AppConstant.DEFAULT_DATE_FORMAT);
		CustomDateEditor editor = new CustomDateEditor(sdf, true);
	}
	
	protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
		binder.registerCustomEditor(Short.class, new CustomNumberEditor(Short.class, true));
		binder.registerCustomEditor(Integer.class, new CustomNumberEditor(Integer.class, true));
		binder.registerCustomEditor(Long.class, new CustomNumberEditor(Long.class, true));
		binder.registerCustomEditor(Float.class, new CustomNumberEditor(Float.class, true));
		binder.registerCustomEditor(Double.class, new CustomNumberEditor(Double.class, true));
		binder.registerCustomEditor(BigDecimal.class, new CustomNumberEditor(BigDecimal.class, true));
		binder.registerCustomEditor(BigInteger.class, new CustomNumberEditor(BigInteger.class, true));

		binder.registerCustomEditor(java.util.Date.class,
				new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));

	}
	
	public static ModelMap toModelMap(Page page, PageRequest pageRequest) {
		return toModelMap("", page, pageRequest);
	}

	public static ModelMap toModelMap(String tableId, Page page, PageRequest pageRequest) {
		ModelMap model = new ModelMap();
		saveIntoModelMap(tableId, page, pageRequest, model);
		return model;
	}
	
	public static void saveIntoModelMap(String tableId, Page page, PageRequest pageRequest, ModelMap model) {
		Assert.notNull(tableId, "tableId must be not null");
		Assert.notNull(page, "page must be not null");

		model.addAttribute(tableId + "page", page);
		model.addAttribute(tableId + "totalRows", new Integer(page.getTotalCount()));
		model.addAttribute(tableId + "pageRequest", pageRequest);
		model.addAttribute(tableId + "query", pageRequest);
	}
	public static void copyProperties(Object target, Object source) {
		BeanUtils.copyProperties(target, source);
	}
	
	
}
